home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * *
- * playpaula.d V1.2 06.09.2000 by Rainer "No.3" Müller *
- * *
- * an example how to use audio-device with the Amiga E Language *
- * *
- * if the file is bigger than 128 KByte, double buffering is used *
- * *
- * compile: dc playpauly *
- * *
- * use: playpaula name name = name of the file you want to replay *
- * *
- * new in: *
- * *
- * V1.0 08.01.1999 first version *
- * *
- * V1.2 06.09.2000 uses now ReadArgs(), checks for OS2.0+ *
- * *
- ************************************************************************/
- OPT OPTIMIZE=-1
-
- MODULE 'lib/amiga','devices/audio','dos/dos','dos/rdargs','exec/io','exec/memory','exec/nodes','exec/ports','graphics/gfxbase'
-
- ENUM ER_NONE, ER_NOOPEN, ER_NOMEM, ER_NOPORT, ER_NOIOREQ, ER_NODEVICE, ER_NOLOAD, ER_BADARGS, ER_KICK
-
- CONST NTSC_CLOCK=3579545,
- PAL_CLOCK=3546895
-
- DEF clock
-
- PROC main()
- DEF fileptr=NIL
- DEF dataptr=NIL:PTR TO CHAR
- DEF datalen=NIL
- DEF rdargs=NIL:PTR TO RDArgs
- DEF myargs
- DEF gfx:PTR TO GfxBase
-
- gfx:=GfxBase
- IF gfx.DisplayFlags & PAL THEN clock:=PAL_CLOCK ELSE clock:=NTSC_CLOCK
- IF KickVersion(37)=NIL THEN Raise(ER_KICK)
- IF (rdargs:=ReadArgs('File/A', &myargs,NIL))=NIL THEN Raise(ER_BADARGS)
- IF (fileptr:=Open(myargs, MODE_OLDFILE))=NIL THEN Raise(ER_NOOPEN)
- datalen:=FileLength(myargs)
- IF (dataptr:=AllocVec(datalen,MEMF_ANY))=NIL THEN Raise(ER_NOMEM)
- IFN Read(fileptr, dataptr, datalen) = datalen THEN Raise(ER_NOLOAD)
-
- playSample(dataptr, datalen)
-
- EXCEPTDO
- IF dataptr THEN FreeVec(dataptr)
- IF fileptr THEN Close(fileptr)
- SELECT exception
- CASE ER_KICK; WriteF('Kickstart V37+ required\n')
- CASE ER_BADARGS; WriteF('bad args\n')
- CASE ER_NOOPEN; WriteF('could not open file\n')
- CASE ER_NOMEM; WriteF('no memory\n')
- CASE ER_NONE; WriteF('all OK\n')
- ENDSELECT
- ENDPROC
-
- PROC playSample(ptr,len)
- DEF arequest1=NIL:PTR TO IOAudio
- DEF arequest2:IOAudio
- DEF reply1=NIL:PTR TO MP
- DEF reply2=NIL:PTR TO MP
- DEF ioa=NIL:PTR TO IOAudio
- DEF mnode=NIL:PTR TO MN
- DEF chipbuf1=NIL:PTR TO CHAR
- DEF chipbuf2=NIL:PTR TO CHAR
- DEF deviceerror=1
- DEF offset
- DEF times, rest, go=0
- DEF samrate=17000
- DEF volume=64 -> Paula's volume range is from 0 (=no volume) to 64 (max volume)
-
- IF (reply1:=CreateMsgPort())=NIL THEN Raise(ER_NOPORT)
- IF (reply2:=CreateMsgPort())=NIL THEN Raise(ER_NOPORT)
- IF (arequest1:=CreateIORequest(reply1, SIZEOF_IOAudio))=NIL THEN Raise(ER_NOIOREQ)
-
- arequest1.IO.MN.LN.Pri:=ADALLOC_MAXPREC -> tell OpenDevice() to allocate one channel
- arequest1.IO.Command:=ADCMD_ALLOCATE
- arequest1.IO.Flags:=ADIOF_NOWAIT
- arequest1.AllocKey:=0
- arequest1.Data:=[1,2,4,8]:CHAR
- arequest1.Length:=4;
- IF deviceerror:=OpenDevice('audio.device', 0, arequest1, 0) THEN Raise(ER_NODEVICE)
-
- arequest1.IO.Command:=CMD_WRITE -> copy iorequest for double buffered operation
- arequest1.IO.Flags:=ADIOF_PERVOL
- arequest1.Period:=Div(clock, samrate)
- arequest1.Volume:=volume
- arequest1.Cycles:=1
- CopyMemQuick(arequest1,arequest2, SIZEOF_IOAudio)
-
- IF len >= 131072 -> sample is bigger than 128 KByte => we have to use double buffering
- IF (chipbuf1:=AllocVec(8192, MEMF_CHIP|MEMF_PUBLIC))=NIL THEN Raise(ER_NOMEM)
- IF (chipbuf2:=AllocVec(8192, MEMF_CHIP|MEMF_PUBLIC))=NIL THEN Raise(ER_NOMEM)
-
- times:=Div(len, 8192)
- rest:=len-Mul(times, 8192)
- times---
- mnode:=arequest2
- mnode.ReplyPort:=reply2
-
- CopyMemQuick(ptr, chipbuf1, 8192) -> send first two buffer's loaded with data to the audio-device
- offset:=8192
- CopyMemQuick(ptr+offset, chipbuf2, 8192)
- offset:=offset + 8192
- arequest1.Data:=chipbuf1
- arequest1.Length:=8192
- arequest2.Data:=chipbuf2
- arequest2.Length:=8192
- BeginIO(arequest1)
- BeginIO(arequest2)
-
- mnode:=arequest1 -> wait on request1 first time around
-
- REPEAT -> until done, keep feeding data to device
- WaitPort(mnode.ReplyPort)
- GetMsg(mnode.ReplyPort)
- ioa:=mnode
- IF ioa=arequest1
- mnode:=arequest2
- ELSE
- mnode:=arequest1
- ENDIF
- IFN times=0
- CopyMemQuick(ptr+offset, ioa.Data, 8192)
- ioa.Length:=8192
- BeginIO(ioa)
- offset:=offset+8192
- DEC times
- ELSEIF times=0 AND rest<>0
- CopyMemQuick(ptr+offset, ioa.Data, rest)
- ioa.Length:=rest
- BeginIO(ioa)
- rest:=0
- ELSE
- go:=1
- WaitPort(mnode.ReplyPort)
- GetMsg(mnode.ReplyPort)
- ENDIF
- UNTIL go=1
- ELSE
- IF (chipbuf1:=AllocVec(len, MEMF_CHIP|MEMF_PUBLIC))=NIL THEN Raise(ER_NOMEM)
-
- mnode:=arequest1
- mnode.ReplyPort:=reply1
- CopyMemQuick(ptr, chipbuf1, len) -> copy sample data to chip-memory
- arequest1.Data:=chipbuf1
- arequest1.Length:=len
- BeginIO(arequest1)
- WaitPort(mnode.ReplyPort)
- GetMsg(mnode.ReplyPort)
- ENDIF
-
- EXCEPTDO
- IF chipbuf2 THEN FreeVec(chipbuf2)
- IF chipbuf1 THEN FreeVec(chipbuf1)
- IF deviceerror=0 THEN CloseDevice(arequest1)
- IF arequest1 THEN DeleteIORequest(arequest1)
- IF reply2 THEN DeleteMsgPort(reply2)
- IF reply1 THEN DeleteMsgPort(reply1)
- SELECT exception
- CASE ER_NOMEM ; WriteF('no memory\n')
- CASE ER_NOPORT ; WriteF('could not create port\n')
- CASE ER_NOIOREQ ; WriteF('could not create iorequest\n')
- CASE ER_NODEVICE; WriteF('could not open audio-device\n')
- CASE ER_NONE ; WriteF('all OK\n')
- ENDSELECT
- ENDPROC
-